import {createStore} from 'vuex'
import {removeToken, setToken} from '../composables/auth'
import {login,getinfo} from '~/api/manager.js'
const store = createStore({
    state(){
        return {
            //用户信息
            user:{
                
            }
        }
    },
    mutations:{
        SET_USERINFO(state,user){
            state.user = user
        }
    },
    actions:{
        login({commit},{username,password}){
            return new Promise((resolve,reject)=>{
                login(username,password).then(res=>{
                    setToken(res.token)
                    resolve(res)
                }).catch(error=>reject(error))
            })
        },
        getinfo({commit}){
            getinfo().then(res=>{
                return new Promise((resolve,reject)=>{
                    getinfo().then(res=>{
                        commit("SET_USERINFO",res)
                        resolve(res)
                    }).catch(error=>reject(error))
                })
            })
        },
        logout({commit}){
            removeToken()
            commit("SET_USERINFO",{})
        }
    }
})
export default store